package com.sbc.user.controller;

import com.sbc.convention.result.Result;
import com.sbc.log.annotation.ILog;
import com.sbc.user.pojo.vo.UserLoginReqVo;
import com.sbc.user.pojo.vo.UserLoginRespVo;
import com.sbc.user.pojo.vo.UserRegisterReqVo;
import com.sbc.user.pojo.vo.UserRegisterRespVo;
import com.sbc.user.service.UserLoginService;
import com.sbc.web.Results;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 用户登录控制层
 * @author: songbaicheng
 * @create: 2024-02-27 20:22
 **/
@ILog
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserLoginController {

    private final UserLoginService userLoginService;

    /**
     * 用户登录
     *
     * @param requestParam 登录请求入参
     * @return 登录结果
     */
    @PostMapping("/v1/login")
    public Result<UserLoginRespVo> login(@Valid @RequestBody UserLoginReqVo requestParam) {
        return Results.success(userLoginService.login(requestParam));
    }

    /**
     * 用户注册
     *
     * @param requestParam 登录请求入参
     * @return 登录结果
     */
    @PostMapping("/v1/register")
    public Result<UserRegisterRespVo> register(@Valid @RequestBody UserRegisterReqVo requestParam) {
        return Results.success(userLoginService.register(requestParam));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping({"/v1/logout/", "/v1/logout/{accessToken}"})
    public Result<Void> logout(@PathVariable(value = "accessToken", required = false) String accessToken) {
        userLoginService.logout(accessToken);
        return Results.success();
    }

}
